
#include <iostream>
#include <string>
#include "zen_image.h"
#include "zen_image_coder.h"
#include "zen_file.h"
#include "zen_utf8.h"
#include "zen_utils.h"
#include "zen_cast.h"
#include "zen_font.h"

using namespace std;
/**
	make text to the image.
	usage
 bin <text>
 */
int main(int argc, const char * argv[]) {

	std::string text_utf8 = "我们的塬BeLoved=\nGod~\nAB CDE FGHIJKL\nMNOPQRSTUVWXYZ";
	if(argc >= 2) text_utf8 = argv[1];

	time_t t;
	time(&t);
	cout << t << endl;
	text_utf8.push_back('\n');
	text_utf8 += ctime(&t);
	Zen::ImagePNGEncoder coder;

	auto buffer = Zen::LoadFileToBuffer("/System/Library/Fonts/Supplemental/Arial Unicode.ttf");

//	buffer.resize(buffer.size() / 5);
//	buffer.assign(1024, 0);

	auto font = Zen::Font::Create(buffer);
	auto brush = Zen::FontBrush::Create(font, 128);
	auto text = Zen::ToU32(text_utf8);
	{
		auto c = brush->getCharBitmap(' ');
		auto c2 = brush->getCharBitmap('j');
//		cout << c->bitmap_x << endl;
		
		Zen::FontRender::Style s;
		s.word_space = 0;
		s.line_space = 0;
		auto render = Zen::FontRender::Create(brush, text, s);
		auto w = render->getOutputWidth();
		auto h = render->getOutputHeight();
		cout << "image size:" << w << " " << h << endl;

		must(w < (1<<12) && h < (1<<12));

		Zen::FontRender::RenderSetting rs;
		rs.underline = 2;
		rs.x_off = 2;
		rs.y_off = 2;
		auto image = render->render(w+4, h+4, rs);

		auto dest = coder.encode(*image);
		if(dest.size())
		{
			Zen::WriteBufferToFile("ft.png", dest);
		}
	}
	return 0;
}
